$(function(){
	/*$(".tj").click(function(){
		if($("input[name='images']").length == 0){
			myAlert("请添加影像");
			return;
		}
		myModalLoadingOpen("正在上传...");
		$.ajax({
			type:"post",
			url:_BASEPATH+"index/upload.shtml",
			data:$("#myForm").serialize(),
			dataType:"JSON",
			success:function(res){
				myModalLoadingClose();
				if(resultCheckFail(res)){

				}else{
					myModalAlert("已上传",function(){
						history.back();
					});
				}
			}
		});
	});*/
});
//用于压缩图片的canvas
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
//瓦片canvas
var tCanvas = document.createElement('canvas');
var tctx = tCanvas.getContext('2d');
//最大200 kb
var maxSize = 200 * 1024;

function removeThis(img){
	$(img).parent().parent().remove();
}
//加载图片
function loadFile(){
	  $c = document.querySelector("#fileBtn");
	  for ( var i = 0; i < $c.files.length; i++) {
		  var file = $c.files[i];
		  var reader = new FileReader();
		  reader.onload = function () {
		    var result = this.result;
		    var img = new Image();
		    img.src = result;
		    if (file.size <= maxSize) {
		    	var li = "<li>"
					+"<div style='background-image:url("+result+")'><span onclick='removeThis(this)' class='delete' src='${basePath}img/delete.png'>删除</span></div>"
					+"<input name='images' type='hidden' value='"+result.split(",")[1]+"'>"
					+"<input name='head' type='hidden' value='"+result.split(",")[0]+"'>"
				+"</li>";
		    	$("#addImg").before(li);
		    	$("#img-list li div").css("height",$("#addImg").css("height"));
		        return;
		    }
		    //压缩
		    if (img.complete) {
		      callback();
		    } else {
		      img.onload = callback;
		    }
		    function callback () {
		      var data = compress(img);
		      var li = "<li>"
						+"<div style='background-image:url("+data+")'><span onclick='removeThis(this)' class='delete' src='${basePath}img/delete.png'>删除</span></div>"
						+"<input name='images' type='hidden' value='"+data.split(",")[1]+"'>"
						+"<input name='head' type='hidden' value='"+data.split(",")[0]+"'>"
					+"</li>";
		      $("#addImg").before(li);
		      $("#img-list li div").css("height",$("#addImg").css("height"));
		    }
		  }
		  reader.readAsDataURL(file);
	  }
}

//压缩方法
function compress (objImg) {
  var initSize = objImg.src.length
  var width = objImg.width
  var height = objImg.height
  // 如果图片大于400万像素，计算压缩比并将大小压至400万以下
  var ratio
  if ((ratio = width * height / 4000000) > 1) {
    ratio = Math.sqrt(ratio)
    width /= ratio
    height /= ratio
  } else {
    ratio = 1
  }
  canvas.width = width
  canvas.height = height
  // 铺底色
  ctx.fillStyle = '#fff'
  ctx.fillRect(0, 0, canvas.width, canvas.height)
  // 如果图片像素大于100万则使用瓦片绘制
  var count
  if ((count = width * height / 1000000) > 1) {
    // 计算要分成多少块瓦片
    count = ~~(Math.sqrt(count) + 1)
    var nw = ~~(width / count)
    var nh = ~~(height / count)
    tCanvas.width = nw
    tCanvas.height = nh
    for (var i = 0; i < count; i++) {
      for (var j = 0; j < count; j++) {
        tctx.drawImage(objImg, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh)
        ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh)
      }
    }
  } else {
    ctx.drawImage(objImg, 0, 0, width, height)
  }
  // 进行最小压缩
  var nData = canvas.toDataURL('image/jpeg', 0.3)
  console.log('压缩前：' + (initSize/1024))
  console.log('压缩后：' + (nData.length/1024))
  console.log('压缩率：' + ~~(100 * (initSize - nData.length) / initSize) + '%')
  tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0
  return nData
}